//
//  UIImage+color.swift
//  ExtremeHealth
//
//  Created by Zhongkai Li 李忠凯 on 2018/8/22.
//  Copyright © 2018 GanDongKeJi. All rights reserved.
//

import UIKit

extension UIImage {
    /// 从颜色生成图片
    ///
    /// - Parameter color: 颜色
    /// - Returns: 图片
    static func from(color: UIColor) -> UIImage {
        let rect = CGRect(x: 0, y: 0, width: 1, height: 1)
        UIGraphicsBeginImageContext(rect.size)
        let context = UIGraphicsGetCurrentContext()
        context!.setFillColor(color.cgColor)
        context!.fill(rect)
        let img = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return img!
    }

    /// 给图片添加透明度
    ///
    /// - Parameter alpha: 透明度
    /// - Returns: 图片
    func image(alpha: CGFloat) -> UIImage? {
        UIGraphicsBeginImageContextWithOptions(size, false, scale)
        draw(at: .zero, blendMode: .normal, alpha: alpha)
        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return newImage
    }
}
